<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\model\CategoryModel;
use app\model\ProductsModel;
use app\model\UserSearchHistoryModel;
use think\App;

class Index extends Base
{

    private $productsModel;

    private $userSearchHistoryModel;

    private $categoryModel;

    private $req;

    private $category_id;

    private $shop_id;

    private $orderConfigList = [
        "default"=>["field"=>"product_sales_num","order"=>"desc"],
        "salesNum"=>["field"=>"product_sales_num","order"=>"desc"],
        "lowerPrice"=>["field"=>"product_min_price","order"=>"asc"],
        "highPrice"=>["field"=>"product_max_price","order"=>"desc"],
        "new"=>["field"=>"p.created_at","order"=>"desc"],
    ];

    private $sortType;

    private $uid;

    private $page;

    private $limit = 20;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;

        $this->uid = $this->getUid();

        $this->sortType = isset($this->req['sortType']) ? $this->req['sortType'] : "default";

        $this->category_id = isset($this->req['category_id']) ?  $this->req['category_id'] : "";

        $this->shop_id = isset($this->req['shop_id']) ?  $this->req['shop_id'] : "";

        $this->productsModel = new ProductsModel();

        $this->categoryModel = new CategoryModel();

        $this->userSearchHistoryModel = new UserSearchHistoryModel();
    }


    /*
     * 获取首页商品列表
     * */
    public function getIndexProductsList()
    {
        $orderSelect = $this->orderConfigList["default"];
        $orderSelect['order'] = isset($this->req['order']) ? $this->req['order'] : $orderSelect["order"];

        $where =[];
        $where[] = ['p.is_delete',"=",0];
        $where[] = ['p.product_status','=',1];
        $where[] = ['category_status','=',1];
        $where[] = ['shop_status','=',1];
        $where[] = ['s.is_delete','=',0];

        $list = $this->productsModel->getList(1,$where,$this->page,$this->limit,$orderSelect['field'],$orderSelect['order'],"default");

        $total = $this->productsModel->getTotal($where);

        return $this->succData($total,$list);
    }


    /*
  * 获取首页商品列表
  * */
    public function getCategoryProductsList()
    {
        $categoryIds =[];
        if(isset($this->req["category_pid"]) && !empty($this->req["category_pid"]) && empty($this->category_id)){
            $categoryIds = $this->categoryModel->getList(
                [
                    "category_status"=>1,
                    "category_pid"=>$this->req["category_pid"]
                ]);
            $categoryIds = array_column($categoryIds,"category_id");
        }else if(isset($this->req["category_pid"]) && empty($this->req["category_pid"])  && empty($this->category_id)){
            $categoryIds = $this->categoryModel->getList(
                [
                    "category_status"=>1,
                ]);
            $categoryIds = array_column($categoryIds,"category_id");
        }


        $orderSelect = $this->orderConfigList[$this->sortType];
        $orderSelect['order'] = isset($this->req['order']) ? $this->req['order'] : $orderSelect["order"];

        $where =[];
        $where[] = ['p.is_delete',"=",0];
        $where[] = ['p.product_status','=',1];
        $where[] = ['category_status','=',1];
        $where[] = ['shop_status','=',1];
        $where[] = ['s.is_delete','=',0];

        if(empty($this->category_id) && !empty($categoryIds)){
            $where[] = ['p.category_id','IN',$categoryIds];
        }else if(!empty($this->category_id)){
            $where[] = ['p.category_id','=',$this->category_id];
        }
        if(isset($this->req['word']) && !empty($this->req['word'])){
            $where[] = ['p.product_name|c.category_name','LIKE',"%{$this->req['word']}%"];
        }

        $list = $this->productsModel->getList(1,$where,$this->page,$this->limit,$orderSelect['field'],$orderSelect['order'],$this->sortType);

        $total = $this->productsModel->getTotal($where);

        return $this->succData($total,$list);
    }




    /*
  * 获取店铺商品列表
  * */
    public function getShopProductsList()
    {

        if(empty($this->shop_id)){
            return $this->fail("参数错误:店铺Id 不能为空");
        }

        $orderSelect = $this->orderConfigList[$this->sortType];
        $orderSelect['order'] = isset($this->req['order']) ? $this->req['order'] : $orderSelect["order"];

        $where =[];
        $where[] = ['p.is_delete',"=",0];
        $where[] = ['p.product_status','=',1];
        $where[] = ['category_status','=',1];
        $where[] = ['shop_status','=',1];
        $where[] = ['s.is_delete','=',0];
        $where[] = ['p.shop_id','=',$this->shop_id];

        $list = $this->productsModel->getList(1,$where,$this->page,$this->limit,$orderSelect['field'],$orderSelect['order'],$this->sortType);

        $total = $this->productsModel->getTotal($where);

        return $this->succData($total,$list);
    }


    /*
   * 搜索功能
   * */
    public function searchProductsList()
    {

        if(empty($this->req['word'])){
            return $this->fail("搜索关键字不能为空");
        }

        if($this->uid && $this->req['word']){
            //添加搜索记录
            $this->userSearchHistoryModel->addOne([
                "uid"=>$this->uid,
                "type"=>1,
                "word"=>$this->req['word']
            ]);
        }


        $orderSelect = $this->orderConfigList[$this->sortType];
        $orderSelect['order'] = isset($this->req['order']) ? $this->req['order'] : $orderSelect["order"];

        $where=[];
        $where[] = ['product_status','=',1];
        $where[] = ['p.is_delete','=',0];
        $where[] = ['category_status','=',1];
        $where[] = ['shop_status','=',1];
        $where[] = ['s.is_delete','=',0];
        $where[] = ['p.product_name|c.category_name','LIKE',"%{$this->req['word']}%"];

        $list = $this->productsModel->getList(1,$where,$this->page,$this->limit,$orderSelect['field'],$orderSelect['order'],$this->sortType);

        $total = $this->productsModel->getTotal($where);

        return $this->succData($total,$list);
    }


}
